using System;
using System.Configuration;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Catalyst.Configuration
{
	[Serializable]
	public class CatalystSection : ConfigurationSection, ISerializable 
	{
		private DatabaseElementCollection databases;

		[ConfigurationProperty("databases", IsDefaultCollection = false)]
		[ConfigurationCollection(typeof(DatabaseElementCollection),
			AddItemName = "add",
			ClearItemsName = "clear",
			RemoveItemName = "remove")]
		public DatabaseElementCollection Databases
		{
			get 
			{
				if (databases == null)
				{
					databases = (DatabaseElementCollection)base["databases"];
				}

				return databases; 
			}
			set { databases = value; }
		}

		#region ISerializable Members

		public void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("Databases", Databases);
		}

		#endregion

		public CatalystSection()
		{
		}

		protected CatalystSection(SerializationInfo info, StreamingContext context)
        {
			if (info == null)
			{
				throw new ArgumentNullException("info");
			}

			databases = (DatabaseElementCollection)info.GetValue("Databases", typeof(DatabaseElementCollection));            
        }

		[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
		void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
		{
			if (info == null)
			{
				throw new ArgumentNullException("info");
			}

			GetObjectData(info, context);
		}
	}
}